﻿using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Hosting;
using System;
using Microsoft.AspNetCore.Builder;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
    public class WebApiService
    {

        public static async Task StartService(int port)
        {
            try
            {
                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseUrls($"http://0.0.0.0:{port}")
                    .ConfigureServices(services =>
                    {
                        services.AddRouting();
                        services.AddControllers();
                    })
                    .Configure(app =>
                    {
                        app.UseRouting();
                        app.UseEndpoints(endpoints =>
                        {
                            endpoints.MapGet("/", async context =>
                            {
                                await context.Response.WriteAsync("Hello World!");
                            });
                            endpoints.MapControllers();     //替换为你的路由配置
                        });
                    })
                    .Build();

                await host.StartAsync();

                Console.WriteLine($"【WEB服务】启动成功: http://0.0.0.0:{port}");

                // 保持应用程序运行，直到主机关闭
                await host.WaitForShutdownAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"【WEB服务】启动失败: {ex}");
            }
        }

        //private IWebHost webHost;

        //public void StartService(int port)
        //{
        //    try
        //    {
        //        //WebApplication1.Program.Main(new string[0]);
        //        //return;
        //        webHost = new WebHostBuilder()
        //            .UseKestrel()
        //            .UseUrls($"http://0.0.0.0:{port}")
        //            .ConfigureServices(services =>
        //            {
        //                // 配置服务
        //                services.AddRouting();
        //                services.AddControllers();
        //            })
        //            .Configure(app =>
        //            {
        //                // 配置中间件
        //                app.UseRouting();
        //                app.UseEndpoints(endpoints =>
        //                {
        //                    endpoints.MapGet("/", async context =>
        //                    {
        //                        await context.Response.WriteAsync("Hello World!");
        //                    });
        //                    endpoints.MapControllers(); // 替换为你的路由配置
        //                });
        //            })
        //            .Build();

        //        webHost.Start();

        //        //_ = $"【WEB服务】启动成功:{webHost.ServerFeatures.Get<IServerAddressesFeature>().Addresses.FirstOrDefault()}".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
        //        while (true)
        //            System.Threading.Thread.Sleep(24 * 60 * 60 * 1000);
        //    }
        //    catch (Exception ex)
        //    {
        //        //$"【WEB服务】启动失败:{ex}".MessageDialogs(MessageLevelType.警告, true, PopUpTypes.Global);
        //        Console.WriteLine(ex.ToString());
        //    }
        //}

        //public void StopService()
        //{
        //    webHost?.StopAsync().Wait();
        //}
    }
}
